//显示商场楼层品牌
(function ($, undefined) {
    $.fn.mall = function (options) {
        var settings = {
            floorUrl:'',
            brandUrl:''
        };
        if (options) {
            $.extend(settings, options);
        }
        var getData = function (url, id, em) {
            $.ajax({
                type:"GET",
                url:url + id,
                dataType:"JSON",
                success:function (data) {
                    if (data.status == 1 && data.data) {
                        var html = "";
                        $.each(data.data, function (i, n) {
                            html += "<option value=\"" + n.id + "\">" + n.name
                                + "</option>";
                        });
                        em.html(html);
                    }
                }
            });
        }
        var market = $(this);
        var floor = $("#floor");
        var brand = $("#brand");
        market.bind('click change', function () {
            floor.html("");
            brand.html("");
            getData(settings.floorUrl, market.val(), floor);
        });
        floor.bind('click change', function () {
            brand.html("");
            getData(settings.brandUrl, floor.val(), brand);
        });
    }
}(jQuery));

// 设置窗口中DIV大小
function resizeHW() {
    var size = 600;
    var height = $(window).height();
    var width = $(window).width();
    if (height > size) {
        $(".l").height(height - 60);
        $(".r").height(height - 60);
    } else {
        $(".l").height(size);
        $(".r").height(size);
    }
    if (width > size) {
        $(".r").width(width - 180);
    } else {
        $(".r").width(size);
    }
}
// 提示信息
function getTip() {
    var i = 1, status = 0;
    var title = document.title;
    var taskReturn = function () {
        $.getJSON(setting.app + '/Robot/Tip/taskReturn', function (data) {
            status = data.status;
            if (!status) {
                document.title = title;
            }
            $("#returnNum").html(data.data.returnNum);
            $("#waitingNum").html(data.data.waitingNum);
            $("#overNum").html(data.data.overNum);
        });
    };
    var returnTitle = function () {
        if (status) {
            if (i == 1) {
                document.title = "[有退回的任务]" + title;
            } else {
                document.title = "[◇◇◇◇◇◇]" + title;
            }
            i = i == 1 ? 2 : 1;
        }
    };
    if (setting.taskTip) {
        taskReturn();
        $('body').everyTime('5s', 'A', taskReturn);
        $('body').everyTime('1s', 'B', returnTitle);
    }
}
// 商场任务显示处理
(function ($, undefined) {
    $.fn.mallTasks = function (options) {
        var settings = {
            mall:3,
            floors:"#floors",
            brands:"#brands",
            add:"a.task_add",
            update:"a.task_update",
            task:"#task"
        };
        if (options) {
            $.extend(settings, options);
        }
        var mid, fid, bid;
        var setFloorClick = function () {
            $(settings.floors + " > a").click(function () {
                if (!$(this).hasClass("on")) {
                    $(settings.floors + " > a").removeClass("on");
                    $(this).addClass("on");
                    // 获取楼层品牌信息
                    getBrands($(this));
                }
            });
        };

        var setTaskAddClick = function (obj) {
            mid = obj.attr('mid');
            fid = obj.attr('fid');
            bid = obj.attr('bid');
            if (mid && fid && bid) {
                $.getJSON(setting.app + '/TaskManage/add/mid/' + mid + '/fid/'
                    + fid + '/bid/' + bid, function (data) {
                    if (data.status) {
                        $(settings.task).html(data.data);
                    } else {
                        alert(data.data);
                    }
                });
            }
        };

        var setTaskUpdateClick = function (obj) {
            var tid = obj.attr('tid');
            if (tid != 0) {
                $.getJSON(setting.app + '/TaskManage/update/id/' + tid,
                    function (data) {
                        if (data.status) {
                            $("#task").html(data.data);
                        } else {
                            alert(data.data);
                        }
                    });
            }
        };

        var setAddAndUpdate = function () {
            $(settings.update).click(function () {
                setTaskUpdateClick($(this));
            });
            $(settings.add).click(function () {
                setTaskAddClick($(this));
            });
        };

        var getBrands = function (obj) {
            var fid = obj.attr('fid');
            if (fid != 0) {
                $(settings.brands).html("加载中...");
                $.getJSON(setting.app + '/TaskManage/getBrands/id/' + fid,
                    function (data) {
                        if (data.status) {
                            $(settings.brands).html(data.data);
                            setAddAndUpdate();
                            $(settings.task).html("");
                        } else {
                            $(settings.brands).html("加载失败！");
                        }
                    });
            } else {
                getMallTasks();
            }
        };

        var getFloors = function () {
            $(settings.task).html("");
            $(settings.brands).html("");
            $.getJSON(
                setting.app + '/TaskManage/getFloors/id/' + settings.mall,
                function (data) {
                    if (data.status) {
                        $(settings.floors).html(data.data);
                        setFloorClick();
                    }
                });
        };
        // 获取商场默认显示任务信息
        var getMallTasks = function () {
            $(settings.task).html("");
            $(settings.brands).html("");
            $.getJSON(setting.app + '/TaskManage/getMallTasks/id/'
                + settings.mall, function (data) {
                if (data.status) {
                    $(settings.brands).html(data.data);
                    setAddAndUpdate();
                }
            });
        };
        if (setting.mid) {
            settings.mall = setting.mid;
            getFloors();
            getMallTasks();
        }
    };
}(jQuery));

$(function () {
    resizeHW();
    resizeHW();
    $(window).resize(function () {
        resizeHW();
    });
    getTip();
});